package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/shopping")
public class ShoppingServlet extends ModelBaseServlet {


    /**
     * 转发到shopping-list.html
     *
     * @param request
     * @param response
     */
    public void toShoppingListPage(HttpServletRequest request, HttpServletResponse response) {
        try {
            processTemplate("shopping-list", request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加历史记录
     *
     * @param request
     * @param response
     */
    public void addHistory(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        System.out.println("ShoppingServlet addHistory " + id);

        Cookie[] cookies = request.getCookies();
        Cookie myCookie = null;
        if (null == cookies || 0 == cookies.length) {
        } else {
            for (Cookie cookie : cookies) {
                if ("history".equals(cookie.getName())) {
                    myCookie = cookie;
                }
            }
        }
        if (null == myCookie) {
            //第一次浏览
            myCookie = new Cookie("history", id);
        } else {
            //不是第一次浏览器 , 商品之前是否浏览过？
            //0-1
            String historyStr = myCookie.getValue();
            if (!historyStr.contains(id)) {
                historyStr = historyStr + "-" + id;
            }
            myCookie.setValue(historyStr);
        }
        response.addCookie(myCookie);


        //展示历史记录
        try {
            response.sendRedirect(request.getContextPath() + "/shopping?method=showHistory");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 展示历史浏览记录
     *
     * @param request
     * @param response
     */
    public void showHistory(HttpServletRequest request, HttpServletResponse response) {
        //获取历史浏览记录
        String[] shoppingNames = {"西游记", "红楼梦", "三国演义", "水浒传"};
        Cookie[] cookies = request.getCookies();
        Cookie myCookie = null;
        if (null == cookies || 0 == cookies.length) {
        } else {
            for (Cookie cookie : cookies) {
                if ("history".equals(cookie.getName())) {
                    myCookie = cookie;
                }
            }
        }
        List<String> shoppingNameList = new ArrayList<>();
        if (null == myCookie) {
            //没有历史记录
        } else {
            //有历史记录 : 0-1-2-3
            String historyStr = myCookie.getValue();
            String[] historyIndexStrs = historyStr.split("-");
            for (String historyIndexStr : historyIndexStrs) {
                Integer historyIndex = Integer.parseInt(historyIndexStr);
                String shoppingName = shoppingNames[historyIndex];
                shoppingNameList.add(shoppingName);
            }
        }

        request.setAttribute("shoppingNameList", shoppingNameList);
        try {
            processTemplate("shopping-history", request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
